// 数组存储类模板定义
#include <iostream>

template <class ArrType>
class MyArray
{
public:
    MyArray(int cap)
    {
        m_array_cap = cap;
        m_array_size = 0;
        m_array = new ArrType[cap];
    }

    // 拷贝构造函数
    MyArray(MyArray&p)
    {
        m_array_cap = p.m_array_cap;
        m_array_size = p.m_array_size;
        m_array = new ArrType[m_array_cap];
        for (int i = 0; i < p.m_array_size; i++)
            m_array[i] = p.m_array[i];
    }
    MyArray &operator=(const MyArray &p)
    {
        if (this == p) // 避免自我赋值
        {
            return *this;
        }

        if (m_array != NULL)
        {
            delete[] m_array;
            m_array_cap = 0;
            m_array_size = 0;
        }

        m_array_cap = p.m_array_cap;
        m_array_size = p.m_array_size;
        m_array = new ArrType[p.m_array_cap];
        for (int i = 0; i < p.m_array_size; i++)
            m_array[i] = p.m_array[i];
        return *this;
    }

    ArrType operator[](int index)
    {
        return m_array[index];
    }

    void push_back(const ArrType &val)
    {
        if (m_array_size == m_array_cap)
        return ;
        m_array[m_array_size] = val;
        m_array_size++;
    }
    void pop_back()
    {
        if (m_array_size == 0)
            return;
        m_array_size--;
    }

    int size()
    {
        return m_array_size;
    }
    int capacity()
    {
        return m_array_cap;
    }

    ~MyArray()
    {
        if (this->m_array != NULL)
        {
            delete[] this->m_array;
            this->m_array = NULL;
            this->m_array_cap = 0;
            this->m_array_size = 0;
        }
    }

public:
    int m_array_cap;
    int m_array_size;
    ArrType *m_array;
};